﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.BusinessLogic.Member
{
    public class DinnerLogic : ABusinessLogic
    {
        public DataTable GetDinnerView(string strWhere,string strOrderBy="")
        {
            string sql = "select D.*,D.Name as DinnerName from T_Dinner as D inner join T_Tree as T on D.TreeId=T.Id ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            if (string.IsNullOrEmpty(strOrderBy)) strOrderBy = " order by D.AddTime desc";
            sql += strOrderBy;
            return Spa.RunSqLcmdDataSet(sql).Tables[0];
        }

        public List<T_Dinner> GetDinners(string where = "")
        {
            if (string.IsNullOrWhiteSpace(where)) where = " 1 = 1 ";
            var data = Spa.Where<T_Dinner>(where).ToList();
            return data;
        }

        public int Save(T_Dinner dinner, List<V_DinnerItem> dinnerItems)
        {
            bool add = true;
            if (string.IsNullOrEmpty(dinner.Id))
            {
                dinner.Id = Guid.NewGuid().ToString("D");
                dinner.AddTime = DateTime.Now;
            }
            else
            {
                add = false;
                var rDinnerItems = Spa.Where<T_DinnerItem>(" DinnerId = '" + dinner.Id + "'").ToList();
                if (rDinnerItems.Any()) Spa.DeleteObjects(rDinnerItems);
            }
            var tDinnerItems = new List<T_DinnerItem>();
            foreach (var dinnerItem in dinnerItems)
            {
                var tDinnerItem = new T_DinnerItem();
                Spa.CopyPropertiesFrom(tDinnerItem, dinnerItem);
                tDinnerItem.DinnerId = dinner.Id;
                tDinnerItems.Add(tDinnerItem);
            }
            //dinner.NeedPay = billItems.Sum(m => m.Amount * m.Price);
            if (add) Spa.AddObject(dinner);
            else Spa.UpdateObject(dinner);
            Spa.AddObjects(tDinnerItems);
            int row = Spa.SaveChanges();
            return row;
        }

        public V_Dinner GetDinnerViewObject(string where, string strOrderBy="")
        {
            string sql = "select T_Dinner.*,T_Tree.Name as TreeName from T_Dinner inner join T_Tree on T_Tree.Id=T_Dinner.TreeId ";
            if (!string.IsNullOrEmpty(where)) sql = sql + where;
            if (string.IsNullOrEmpty(strOrderBy)) strOrderBy = " order by T_Dinner.AddTime desc";
            sql += strOrderBy;
            var data = Spa.SelectWhere<V_Dinner>(sql).ToList();
            return data.FirstOrDefault();
        }
        public List<V_DinnerItem> GetDinnerItemView(string where)
        {
            string sql = "select T_DinnerItem.*,T_Product.Name as ItemName from T_DinnerItem inner join T_Product on T_Product.Id=T_DinnerItem.ObjectId ";
            if (!string.IsNullOrEmpty(where)) sql = sql + where;
            var data = Spa.SelectWhere<V_DinnerItem>(sql).ToList();
            return data;
        }
    }
}
